package com.sicpa.system.service.impl;

import java.util.List;
import java.util.Map;

import com.sicpa.common.core.domain.entity.SysUser;
import com.sicpa.common.core.domain.model.LoginUser;
import com.sicpa.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sicpa.system.domain.SysNotice;
import com.sicpa.system.mapper.SysNoticeMapper;
import com.sicpa.system.service.ISysNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告 服务层实现
 *
 * @author wph
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService {

    private SysNoticeMapper noticeMapper;

    @Autowired
    public SysNoticeServiceImpl(SysNoticeMapper noticeMapper) {
        this.noticeMapper = noticeMapper;
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 通知公告
     *
     * @param
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        notice.setCreateBy(loginUser.getUsername());
        notice.setInformedPersonId(loginUser.getUser().getUserId());
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 查询公告详情
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(int noticeId) {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 修改是否已读状态为已读
     *
     * @param noticeId 需要修改状态的公告ID
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStatus(int noticeId) {
        SysNotice sysNotice = noticeMapper.selectNoticeById(noticeId);
        int i = 0;
        //如果类型为1 则为个人消息
        if (sysNotice.getNoticeType().equals("1")) {
            i = setIsReadAndStatus(sysNotice);
        }
        //否则,公告类型为2 ,为全部公告
        else {
            i = setIsReadAndStatus(sysNotice);
            //因为全部用户公告,故修改状态时查询中间表公告ID和被通知人ID对应数据,判断未读则新增
            int informedPersonId = Math.toIntExact(SecurityUtils.getLoginUser().getUser().getUserId());
            //查询是否新增过,未新增过则新增中间表数据 代表已读
            if (noticeMapper.selectSysNoticeUser(informedPersonId, noticeId) == 0) {
                noticeMapper.insertSysNoticeUser(informedPersonId, noticeId);
            }
        }
        return i;
    }

    /**
     * 修改是否已读状态和状态公用方法
     *
     * @param tempNotice
     * @return
     */
    private int setIsReadAndStatus(SysNotice tempNotice) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setNoticeId(tempNotice.getNoticeId());
        //如果是否已读为0未读,则将是否已读改为1已读
        if (tempNotice.getIsRead().equals("0")) {
            sysNotice.setIsRead("1");
        }
        //判断,如果公告状态为0 未发送 ,则将公告状态改为1 已发送
        if (tempNotice.getStatus().equals("0")) {
            sysNotice.setStatus("1");
        }
        //更新是否已读和状态
        return noticeMapper.updateNotice(sysNotice);
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(int noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(int[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    /**
     * 根据姓名模糊查询用户列表
     *
     * @param name 用户名
     * @return 用户名和用户ID的集合
     */
    @Override
    public List<Map<String, Object>> selectUserList(String name) {
        return noticeMapper.selectUserList(name);
    }

    /**
     * 站内信通知个人
     * @param userId
     * @param noticeTitle
     * @param noticeContent
     * @param relatedItem
     * @param relatedId
     * @return
     */
    @Override
    public int stationLetterNotify(Long userId, String noticeTitle, String noticeContent,String relatedItem,Long relatedId){
        SysNotice sysNotice = new SysNotice();
        sysNotice.setStatus("1");
        sysNotice.setNoticeTitle(noticeTitle);
        sysNotice.setNoticeContent(noticeContent);
        sysNotice.setInformedPersonId(userId);
        sysNotice.setRelatedId(relatedId);
        sysNotice.setCreateBy("System");
        sysNotice.setRelatedItem(relatedItem);
        sysNotice.setNoticeType("1");
        return noticeMapper.insertNotice(sysNotice);
    }

    /**
     * 获取我的未读消息数量
     *
     * @return
     */
    @Override
    public Integer getMyNoRead() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long userId = sysUser.getUserId();
        Integer num = 0;
        Integer countNoticeNoRead = noticeMapper.selectCountNoticeNoRead(userId,sysUser.getUserName());
        if (countNoticeNoRead != null) {
            num += countNoticeNoRead;
        }
        Integer countPersonalNoRead = noticeMapper.selectCountPersonalNoRead(userId);
        if (countPersonalNoRead != null) {
            num += countPersonalNoRead;
        }
        return num;
    }
}
